$(function () {
    renderUser()
})

async function getUserById() {
    let { id } = getParams()
    if (!id) {
        return null
    }
    //准备参数
    let options = {
        url: 'http://146.56.230.35:3007/bill/getBillById',
        type: 'GET',
        data: { id: parseInt(id) },
    }
    try {
        let res = await request(options)
        if (res.code !== 200) {
            alert(res.msg)
            return null
        }
        return res
    } catch (error) {
        return null
    }
}

async function renderUser() {
    let res = await getUserById()
    let providerView = $('.providerView')
    providerView.empty()
    let pay
    if (res.data.isPayment === 1) {
        pay = '已付款'
    } else {
        pay = '未付款'
    }
    let str = `
      <p><strong>账单编号：</strong><span>${res.data.billCode}</span></p>
      <p><strong>商品名称：</strong><span>${res.data.productName}</span></p>
      <p><strong>商品单位：</strong><span>${res.data.productUnit}</span></p>
      <p><strong>商品数量：</strong><span>${res.data.productCount}</span></p>
      <p><strong>商品价格：</strong><span>${res.data.totalPrice}</span></p>
      <p><strong>供应商编号：</strong><span>${res.data.providerId}</span></p>
      <p><strong>付款：</strong><span>${pay}</span></p>
      <a href="billList.html">返回</a>
    `
    providerView.html(str)
}

//获得url中的查询参数
function getParams() {
    //?id=40&name=tom
    let search = location.search
    if (!search) {
        return null
    }
    search = search.substring(1)
    let index = search.indexOf('&')
    let object = {}
    if (index === -1) {
        let arr = search.split('=')
        object[arr[0]] = arr[1]
    } else {
        //id=40&name=tom--->arr=['id=40','name=tom']
        let arr = search.split('&')
        for (let i = 0; i < arr.length; i++) {
            //'id=40'
            let item = arr[i]
            //'id=40'--->['id','40']
            let array = item.split('=')
            object[array[0]] = array[1]
        }
    }
    return object
}